package leaf

// reference: https://github.com/name5566leaf
import (
	"leaf/console"
	"leaf/log"
	"leaf/module"
	"os"
	"os/signal"
)

type RunArgs struct {
	ConsolePort int
	ProfilePath string
}

func Run(args *RunArgs, modules ...module.Module) {
	log.Release("Leaf %v starting up", version)

	if args == nil {
		args = new(RunArgs)
	}

	// close signal
	closeSignal := make(chan os.Signal, 1)
	signal.Notify(closeSignal, os.Interrupt, os.Kill)

	// module
	for i := 0; i < len(modules); i++ {
		module.Register(modules[i])
	}
	module.Prepare()
	module.Init()
	defer module.Destroy()

	// console
	console.Init(closeSignal, args.ConsolePort, args.ProfilePath)
	defer console.Destroy()

	// close
	sig := <-closeSignal
	log.Release("Leaf closing down (signal: %v)", sig)
}
